package cn.lau.pp.module.vr.controller.admin.game;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.lau.pp.framework.common.pojo.PageParam;
import cn.lau.pp.framework.common.pojo.PageResult;
import cn.lau.pp.framework.common.pojo.CommonResult;
import cn.lau.pp.framework.common.util.object.BeanUtils;
import static cn.lau.pp.framework.common.pojo.CommonResult.success;

import cn.lau.pp.framework.excel.core.util.ExcelUtils;

import cn.lau.pp.framework.operatelog.core.annotations.OperateLog;
import static cn.lau.pp.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.lau.pp.module.vr.controller.admin.game.vo.*;
import cn.lau.pp.module.vr.dal.dataobject.game.GameDO;
import cn.lau.pp.module.vr.service.game.GameService;

@Tag(name = "管理后台 - vr游戏")
@RestController
@RequestMapping("/vr/game")
@Validated
public class GameController {

    @Resource
    private GameService gameService;

    @PostMapping("/create")
    @Operation(summary = "创建vr游戏")
    @PreAuthorize("@ss.hasPermission('vr:game:create')")
    public CommonResult<Integer> createGame(@Valid @RequestBody GameSaveReqVO createReqVO) {
        return success(gameService.createGame(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新vr游戏")
    @PreAuthorize("@ss.hasPermission('vr:game:update')")
    public CommonResult<Boolean> updateGame(@Valid @RequestBody GameSaveReqVO updateReqVO) {
        gameService.updateGame(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除vr游戏")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vr:game:delete')")
    public CommonResult<Boolean> deleteGame(@RequestParam("id") Integer id) {
        gameService.deleteGame(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得vr游戏")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vr:game:query')")
    public CommonResult<GameRespVO> getGame(@RequestParam("id") Integer id) {
        GameDO game = gameService.getGame(id);
        return success(BeanUtils.toBean(game, GameRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得vr游戏分页")
    @PreAuthorize("@ss.hasPermission('vr:game:query')")
    public CommonResult<PageResult<GameRespVO>> getGamePage(@Valid GamePageReqVO pageReqVO) {
        PageResult<GameDO> pageResult = gameService.getGamePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GameRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出vr游戏 Excel")
    @PreAuthorize("@ss.hasPermission('vr:game:export')")
    @OperateLog(type = EXPORT)
    public void exportGameExcel(@Valid GamePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GameDO> list = gameService.getGamePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "vr游戏.xls", "数据", GameRespVO.class,
                        BeanUtils.toBean(list, GameRespVO.class));
    }

}